package com.whut.cristiano.autographbook.Activity;

import android.Manifest;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.design.widget.TextInputLayout;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.whut.cristiano.autographbook.Model.HandlePictureListener;
import com.whut.cristiano.autographbook.R;
import com.whut.cristiano.autographbook.Util.ImageUtil;
import com.whut.cristiano.autographbook.Util.LogUtil;
import com.whut.cristiano.autographbook.Util.NetUtil;
import com.whut.cristiano.autographbook.Util.SharedPrefUtil;
import com.whut.cristiano.autographbook.Util.ToastUtil;
import com.yyx.beautifylib.model.BLPickerParam;
import com.yyx.beautifylib.model.BLResultParam;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;
import pub.devrel.easypermissions.EasyPermissions;

public class ChangeInfoActivity extends BaseActivity {
    private String TAG = ChangeInfoActivity.class.getSimpleName();

    private CircleImageView circleImageView;
    private ProgressDialog progressDialog;
    private TextInputLayout userNameWrapper;
    private FloatingActionButton floatingActionButton;
    private EditText changeUserNameEdit;
    private ImageUtil imageUtil;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_info);
        initView();
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case android.R.id.home:
                finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }
    @Override
    public void initView() {
        super.initView();
        floatingActionButton = (FloatingActionButton) findViewById(R.id.submit_info);
        floatingActionButton.setOnClickListener(this);
        changeUserNameEdit = (EditText) findViewById(R.id.activity_change_user_name_edit);
        changeUserNameEdit.setText(SharedPrefUtil.getUserName());
        userNameWrapper = (TextInputLayout) findViewById(R.id.userNameWrapper);
        userNameWrapper.setHint("修改昵称");
        progressDialog = new ProgressDialog(this);
        circleImageView = (CircleImageView) findViewById(R.id.activity_change_info_photo);
        Glide.with(this).load(SharedPrefUtil.getUserPhoto()).into(circleImageView);
        circleImageView.setOnClickListener(this);
        imageUtil = new ImageUtil(circleImageView,ChangeInfoActivity.this);

    }
    private void gotoPhotoPickActivity() {
        String[] perms = {Manifest.permission.WRITE_EXTERNAL_STORAGE};
        if (EasyPermissions.hasPermissions(this, perms)) {
            BLPickerParam.startActivity(ChangeInfoActivity.this);
        } else {
            EasyPermissions.requestPermissions(this, "图片选择需要以下权限:\n\n1.访问读写权限",1, perms);
        }
    }
    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()){
            case R.id.activity_change_info_photo:
                imageUtil.choosePhoto();
                break;
            case R.id.submit_info:
                if (changeUserNameEdit.getText().toString().trim().equals("")){
                    Snackbar.make(v,"违规昵称。",Snackbar.LENGTH_LONG).setAction("确定", new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {}
                    }).show();
                    break;
                }
                if ((SharedPrefUtil.getUserPhotoTempPath()==null||SharedPrefUtil.getUserPhotoTempPath().equals(""))&&changeUserNameEdit.getText().toString().trim().equals(SharedPrefUtil.getUserName())){
                    Snackbar.make(v,"请修改信息再提交。",Snackbar.LENGTH_LONG).setAction("确定", new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {}
                    }).show();
                    break;
                }else{
                    showDialog(progressDialog);
                    String t = SharedPrefUtil.getUserPhotoTempPath();
                    if (t==null||t.equals("")){
                        //只修改了用户名
                        NetUtil.updateUserInfo(SharedPrefUtil.getUserPhone(),SharedPrefUtil.getUserPhoto()
                        ,changeUserNameEdit.getText().toString(),progressDialog,floatingActionButton);
                        break;
                    }else {
                        //头像修改了
                        ImageUtil.TemplateUploadPhoto(progressDialog, new HandlePictureListener() {
                            @Override
                            public void onStart() {}

                            @Override
                            public void onStop(String picUrl, Boolean isUploaded, String time) {
                                progressDialog.dismiss();
                                if (isUploaded){
                                    showDialogY(progressDialog);
                                    NetUtil.updateUserInfo(SharedPrefUtil.getUserPhone(),
                                            picUrl,
                                            changeUserNameEdit.getText().toString()==null?SharedPrefUtil.getUserName():changeUserNameEdit.getText().toString(),
                                            progressDialog,
                                            floatingActionButton);
                                }

                            }

                            @Override
                            public void onError(Exception e) {}
                        },SharedPrefUtil.getUserPhotoTempPath());
                    }


                }
        }
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch(requestCode){
            case ImageUtil.TAKE_PIC:
                if(resultCode==RESULT_OK){
                    //将拍摄的照片进行裁剪
                    imageUtil.cropPic();
                    break;
                }
            case ImageUtil.COMPLETE_CROP_CODE:
                imageUtil.setImageToView(data);
                break;
            case ImageUtil.SELECT_PIC_KITKAT:
                if (data==null){//如果没有选择图片
                    break;
                }else {
                    imageUtil.bitmapFactory(data.getData());
                    break;
                }
            case ImageUtil.IMAGE_REQUEST_CODE:
                imageUtil.startPhotoZoom(data.getData());
                break;
            case BLPickerParam.REQUEST_CODE_PHOTO_PICKER:
                BLResultParam param = data.getParcelableExtra(BLResultParam.KEY);
                List<String> imageList = param.getImageList();
                StringBuilder sb = new StringBuilder();
                for (String path:imageList){
                    sb.append(path);
//                    sb.append("\n");
                }
                ToastUtil.showToast(this,sb.toString());
                LogUtil.d("OSSUtil",sb.toString());
                SharedPrefUtil.saveTemplateOnePicUrl(sb.toString());
                break;
        }
    }
    public void showDialogY(ProgressDialog progressDialog) {
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.setIndeterminate(false);
        progressDialog.setCancelable(false);
        progressDialog.setMessage("正在修改个人信息...");
        progressDialog.show();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        SharedPrefUtil.saveUserPhotoTempPath(null);
    }
}
